using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using TienTeTuong;
using log4net;
using TienTeTuong.DTO;

namespace TienTeTuong
{
    public class ArticleBUS
    {
        
        public List<ArticleDTO> GetArticleByViewed(int top)
        {
            var dao = new ArticleDAO();
            return dao.GetArticleByViewed(top);
        }


        public ArticleDTO GetArticleDetailById(string id)
        {
            var dao = new ArticleDAO();
            return dao.GetArticleDetailByID(id);
        }

        public List<ArticleDTO> GetNewArticles(int top, string exceptArticle)
        {
            var dao = new ArticleDAO();
            return dao.GetNewArticles(top, exceptArticle);
        }
        public List<ArticleDTO> GetArticleByPage(string cateArtID,int numPage, out int iTotalPages, out int iCurPage, out int iLowerPage, out int iUpperPage, out int iTotalResult)
        {
            iCurPage = numPage;
            var dao = new ArticleDAO();
            iTotalResult = dao.GetNumArticles(cateArtID);

            iTotalPages = iTotalResult / Common.numItemArticlePerPage + (iTotalResult % Common.numItemArticlePerPage > 0 ? 1 : 0);
            if (iCurPage < 1) iCurPage = 1;
            if (iCurPage > iTotalPages) iCurPage = iTotalPages;
            Common.CalculateBoundry(Common.numItemArticlePerPage, iTotalPages, iCurPage, out iLowerPage, out iUpperPage);
            var offsetFirstRow = (iCurPage - 1) * Common.numItemArticlePerPage;
            if (offsetFirstRow < 0) offsetFirstRow = 0;

            return dao.GetArticleByPage(cateArtID, offsetFirstRow, Common.numItemArticlePerPage);
        }
        public List<ArticleDTO> SearchArticleByPage(string keyword, int numPage, out int iTotalPages, out int iCurPage, out int iLowerPage, out int iUpperPage, out int iTotalResult)
        {
            iCurPage = numPage;
            var dao = new ArticleDAO();
            iTotalResult = dao.GetSearchNumArticles(keyword);

            iTotalPages = iTotalResult / Common.numItemArticlePerPage + (iTotalResult % Common.numItemArticlePerPage > 0 ? 1 : 0);
            if (iCurPage < 1) iCurPage = 1;
            if (iCurPage > iTotalPages) iCurPage = iTotalPages;
            Common.CalculateBoundry(Common.numItemArticlePerPage, iTotalPages, iCurPage, out iLowerPage, out iUpperPage);
            var offsetFirstRow = (iCurPage - 1) * Common.numItemArticlePerPage;
            if (offsetFirstRow < 0) offsetFirstRow = 0;

            return dao.SearchArticleByPage(keyword, offsetFirstRow, Common.numItemArticlePerPage);
        }
           public List<ArticleDTO> GetListArticleDetailByID(string cateArtID)
           {
               var dao = new ArticleDAO();
               return dao.GetListArticleDetailByID(cateArtID);
           }
        public List<ArticleDTO> GetArticleDetailByTime(string month, string year)
        {
            var dao = new ArticleDAO();
            return dao.GetArticleDetailByTime(month, year);
        }
        public List<CommentDTO> GetCommentByArticleID(string artID, int top)
        {
            var dao = new ArticleDAO();
            return dao.GetCommentByArticleID(artID, top);
        }
        public bool InsertNewCommentByArticleID(string artID, CommentDTO cmt)
        {
            var dao = new ArticleDAO();
            return dao.InsertNewCommentByArticleID(artID, cmt);
        }

        public bool UpdateViewedNumberByArticleID(string artID, double viewedNumber)
        {
            var dao = new ArticleDAO();
            return dao.UpdateViewedNumberByArticleID(artID, viewedNumber);
        }
        public List<ArticleDTO> GetAllArticle()
        {
            var dao = new ArticleDAO();
            return dao.GetAllArticle();
        }
        public bool delete(string key)
        {
            var dao = new ArticleDAO();
            return dao.delete(key);
        }
        public bool update(ArticleDTO item)
        {
            var dao = new ArticleDAO();
            return dao.update(item);
        }

        public bool add(ArticleDTO item)
        {
            var dao = new ArticleDAO();
            return dao.add(item);
        }
    }
}
